import {
	defineStore
} from 'pinia';
import {
	ref
} from 'vue';
export const useCubeStore = defineStore('cube', () => {
	//设备mac地址
	const address = ref('');
	//连接状态: 是否连接
	const isConnected = ref(true);
	//连接状态: 是否正在连接
	const isConnecting = ref(false);
	const connectingName = ref('');
	//扫描状态
	const isScanning = ref(false);
	//设备名称
	const name = ref('');
	//设备电量百分比
	const battery = ref(-1);
	//充电状态: 0放电 1充电 2充电完成
	const batteryType = ref(0);
	//产品型号
	const model = ref('');
	//固件版本号
	const firmwareVersion = ref('');
	//硬件版本号
	const hardwareVersion = ref('');
	//协议版本号
	const protocolVersion = ref('1');
	const gyroEnabled = ref(false);
	const gyroCorrect = ref(false);
	//设备扫描列表
	const devices = ref({});
	//步数列表
	const moveList = ref([]);
	//步数时间间隔列表
	const moveTimeList = ref([]);
	//当前步数序数
	const currentStepNum = ref(-1);
	//当前转动的面
	const currentStepMove = ref(-1);
	//当前转动的公式
	const currentMove = ref({
		move: -1,
		interval: -1
	});
	//丢步次数
	const lostStep = ref(0);
	//是否复原完成
	const isResolved = ref(false);
	//魔方54块状态
	const cubeFace = ref("UUUUUUUUURRRRRRRRRFFFFFFFFFDDDDDDDDDLLLLLLLLLBBBBBBBBB");
	// 陀螺仪数据
	let gyroscopeArr = ref([])
	// 魔方初始步数
	let initStep = 0;
	// 最新的步数
	let newestStep = 0;
	// 魔方当前步数
	let currentStep = 0;
	// 初始化是否完成
	let initState = true;
	// 丢步的公式列表
	let lostStepList = [];
	// 丢步的查询公式列表
	// 计步器：{step: 计步器 startTs: 开始时间戳}
	let lostStepPendingList = [];
	// 计步器：{move: 公式， interval: 间隔时间}
	let moveMap = new Map();
	// 丢步历史
	let lostStepHistory = [];
	// 开启丢步
	let dropOpen = false;
	
	// const connectStatus = computed(() => count.value * 2)
	function connect(mac) {
		return new Promise((resolve, reject) => {
			var _a;
			isConnecting.value = true;
			connectingName.value = (_a = devices.value.get(mac)) === null || _a === void 0 ? void 0 : _a.name;
			setTimeout(() => {
				if (isConnecting.value && !isConnected.value) {
					isConnecting.value = false;
					// connectingName.value = ''
				}
			}, 15000);
			resolve();
		});
	}

	function onConnect(mac) {
		address.value = mac;
		isConnected.value = true;
		isConnecting.value = false;
		isScanning.value = false;
		name.value = connectingName.value;
		if (name.value.toLowerCase().includes("ganicv2s")) {
			protocolVersion.value = '2';
		} else {
			protocolVersion.value = '1';
		}
		//TODO: 从设备名称中解析出设备型号和固件版本号
		//TODO: 从设备名称中解析出协议版本号
	}

	function onDisconnect() {
		address.value = '';
		name.value = '';
		isConnected.value = false;
		isConnecting.value = false;
		isScanning.value = false;
		battery.value = -1;
		batteryType.value = 0;
		model.value = '';
		firmwareVersion.value = '';
		hardwareVersion.value = '';
		protocolVersion.value = '1';
		gyroEnabled.value = false;
		gyroCorrect.value = false;
		moveList.value = [];
		moveTimeList.value = [];
		currentStepNum.value = -1;
		currentStepMove.value = -1;
		currentMove.value = {
			move: -1,
			interval: -1
		};
		lostStep.value = 0;
		isResolved.value = false;
		cubeFace.value = "UUUUUUUUURRRRRRRRRFFFFFFFFFDDDDDDDDDLLLLLLLLLBBBBBBBBB";
		connectingName.value = '';
	}
	/**
	 * 清除历史记录,减少内存占用,目前是每轮结算后清除
	 */
	function clearHistory() {
		lostStepHistory = [];
		lostStepList = [];
		lostStepPendingList = [];
		moveMap = new Map();
		//步数列表
		moveList.value = [];
		//步数时间间隔列表
		moveTimeList.value = [];
	}

	function onFace(data) {
		cubeFace.value = data;
	}

	function onBattery(data) {
		batteryType.value = data[1];
		battery.value = data[2];
	}

	function onMove(data) {
		let stepNum = data.step;
		let duration = data.duration || data.time
		let name = data.cubeName
		currentStepNum.value = stepNum;
		currentStepMove.value = name;
		moveList.value.push(name);
		moveTimeList.value.push(duration);
		currentMove.value = {
			move: name,
			interval: duration
		};
	}

	function onResolved(data) {
		isResolved.value = data;
	}

	function getNow() {
		return Math.floor(Date.now() / 1000);
	}
	
	function setGyroscopes(data) {
		gyroscopeArr.value = [...gyroscopeArr.value, data]
	}
	
	function clearGyroscopesFisrt() {
		gyroscopeArr.value.shift()
	}

	return {
		address,
		name,
		isConnected,
		isConnecting,
		devices,
		isScanning,
		connect,
		onDisconnect,
		clearHistory,
		onMove,
		moveList,
		moveTimeList,
		currentStepMove,
		currentStepNum,
		lostStep,
		currentMove,
		battery,
		batteryType,
		model,
		firmwareVersion,
		hardwareVersion,
		protocolVersion,
		cubeFace,
		onFace,
		onBattery,
		gyroEnabled,
		gyroCorrect,
		onResolved,
		setGyroscopes,
		clearGyroscopesFisrt,
		gyroscopeArr,
	};
});